/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddNewDriver.java
 *
 * Created on Oct 2, 2014, 4:00:51 PM
 */
package com.JD.master.frame;

import com.JD.datePicker.DatePicker;
import com.JD.master.Hibernate.DriverMaster;
import java.awt.print.PrinterException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;

/**
 *
 * @author admin
 */
public class AddNewDriver extends javax.swing.JFrame {

    /** Creates new form AddNewDriver */
    public AddNewDriver() {
        initComponents();

        com.JD.datePicker.DatePicker datePicker = new DatePicker();
        List year_List = datePicker.getYear_List();
        List day_List = datePicker.getDay_List();


        for (Object object : year_List) {

            year_Combobox.addItem(object.toString());
        }

        for (Object object : day_List) {

            date_ComboBox.addItem(object.toString());

        }

        String month[] = {"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"};

        month_Combobox.addItem(month[Integer.parseInt(new java.text.SimpleDateFormat("MM").format(new Date())) - 1]);
        for (String string : month) {

            month_Combobox.addItem(string);
        }

        SessionFactory sessionFactory = com.JD.master.Hibernate.Master_HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();

        //com.JD.master.Hibernate.DriverMaster

        Query query = session.createQuery("from com.JD.master.Hibernate.DriverMaster");

        List list = query.list();
        javax.swing.table.DefaultTableModel model = (javax.swing.table.DefaultTableModel) driver_Table.getModel();



        for (Object object : list) {

            com.JD.master.Hibernate.DriverMaster driverMaster=(com.JD.master.Hibernate.DriverMaster)object;
              model.insertRow(0, new Object[]{driverMaster.getDriverName(), driverMaster.getDriverAddress(), driverMaster.getDriverPhone()});

        }


    }
    com.JD.master.Validation.Validator validator = new com.JD.master.Validation.Validator();
    String driver_name = "NO";
    String driver_Address = "NO";
    String driver_Phone = "NO";
    String rawField1 = "NO";
    String rawField2 = "NO";
    String rawField3 = "NO";
    String rawField4 = "NO";

    public JPanel getAddNewDriver_Panel() {
        return AddNewDriver_Panel;
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        AddNewDriver_Panel = new javax.swing.JPanel();
        jScrollPane1 = new javax.swing.JScrollPane();
        driver_Table = new javax.swing.JTable();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        driverName_Textfield = new javax.swing.JTextField();
        phone_Textfield = new javax.swing.JTextField();
        jScrollPane2 = new javax.swing.JScrollPane();
        address_Textfield = new javax.swing.JTextArea();
        addDataToDatabase_Button = new javax.swing.JButton();
        year_Combobox = new javax.swing.JComboBox();
        month_Combobox = new javax.swing.JComboBox();
        date_ComboBox = new javax.swing.JComboBox();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);

        AddNewDriver_Panel.setBackground(new java.awt.Color(255, 102, 51));
        AddNewDriver_Panel.setPreferredSize(new java.awt.Dimension(1135, 627));

        driver_Table.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {

            },
            new String [] {
                "Driver Name", "Address", "Phone"
            }
        ) {
            Class[] types = new Class [] {
                java.lang.String.class, java.lang.String.class, java.lang.String.class
            };
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public Class getColumnClass(int columnIndex) {
                return types [columnIndex];
            }

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(driver_Table);

        jLabel1.setFont(new java.awt.Font("Traditional Arabic", 1, 24));
        jLabel1.setForeground(new java.awt.Color(18, 68, 200));
        jLabel1.setText("ADD DRIVER DETAILS");

        jLabel2.setFont(new java.awt.Font("Traditional Arabic", 1, 18));
        jLabel2.setForeground(new java.awt.Color(18, 68, 200));
        jLabel2.setText("Driver Name: ");

        jLabel3.setFont(new java.awt.Font("Traditional Arabic", 1, 18));
        jLabel3.setForeground(new java.awt.Color(18, 68, 200));
        jLabel3.setText("Address: ");

        jLabel4.setFont(new java.awt.Font("Traditional Arabic", 1, 18));
        jLabel4.setForeground(new java.awt.Color(18, 68, 200));
        jLabel4.setText("Phone:");

        driverName_Textfield.setFont(new java.awt.Font("Traditional Arabic", 0, 18));
        driverName_Textfield.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                driverName_TextfieldKeyReleased(evt);
            }
        });

        phone_Textfield.setFont(new java.awt.Font("Traditional Arabic", 0, 18));
        phone_Textfield.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                phone_TextfieldActionPerformed(evt);
            }
        });
        phone_Textfield.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                phone_TextfieldKeyReleased(evt);
            }
        });

        address_Textfield.setColumns(20);
        address_Textfield.setFont(new java.awt.Font("Traditional Arabic", 0, 18));
        address_Textfield.setLineWrap(true);
        address_Textfield.setRows(5);
        address_Textfield.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                address_TextfieldKeyReleased(evt);
            }
        });
        jScrollPane2.setViewportView(address_Textfield);

        addDataToDatabase_Button.setFont(new java.awt.Font("Traditional Arabic", 1, 14));
        addDataToDatabase_Button.setText("Add Data");
        addDataToDatabase_Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                addDataToDatabase_ButtonActionPerformed(evt);
            }
        });

        year_Combobox.setEditable(true);

        month_Combobox.setEditable(true);
        month_Combobox.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                month_ComboboxActionPerformed(evt);
            }
        });

        date_ComboBox.setEditable(true);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/JD/PlaceOrder/excel.png"))); // NOI18N
        jButton1.setText("Export To Excel");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });

        jButton2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/com/JD/PlaceOrder/print.png"))); // NOI18N
        jButton2.setText("Print");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout AddNewDriver_PanelLayout = new javax.swing.GroupLayout(AddNewDriver_Panel);
        AddNewDriver_Panel.setLayout(AddNewDriver_PanelLayout);
        AddNewDriver_PanelLayout.setHorizontalGroup(
            AddNewDriver_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(AddNewDriver_PanelLayout.createSequentialGroup()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 1131, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(41, Short.MAX_VALUE))
            .addGroup(AddNewDriver_PanelLayout.createSequentialGroup()
                .addGap(251, 251, 251)
                .addGroup(AddNewDriver_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(AddNewDriver_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(driverName_Textfield, javax.swing.GroupLayout.DEFAULT_SIZE, 445, Short.MAX_VALUE)
                    .addComponent(phone_Textfield, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 445, Short.MAX_VALUE)
                    .addGroup(AddNewDriver_PanelLayout.createSequentialGroup()
                        .addGroup(AddNewDriver_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(AddNewDriver_PanelLayout.createSequentialGroup()
                                .addComponent(year_Combobox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 50, Short.MAX_VALUE)
                                .addComponent(month_Combobox, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addGroup(AddNewDriver_PanelLayout.createSequentialGroup()
                                .addComponent(addDataToDatabase_Button)
                                .addGap(203, 203, 203)))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(AddNewDriver_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(date_ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 143, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jButton1)))
                    .addComponent(jScrollPane2, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.DEFAULT_SIZE, 445, Short.MAX_VALUE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(jButton2)
                .addGap(262, 262, 262))
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, AddNewDriver_PanelLayout.createSequentialGroup()
                .addContainerGap(452, Short.MAX_VALUE)
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 292, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(428, 428, 428))
        );
        AddNewDriver_PanelLayout.setVerticalGroup(
            AddNewDriver_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, AddNewDriver_PanelLayout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1, javax.swing.GroupLayout.PREFERRED_SIZE, 29, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 14, Short.MAX_VALUE)
                .addGroup(AddNewDriver_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(driverName_Textfield, javax.swing.GroupLayout.PREFERRED_SIZE, 35, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(AddNewDriver_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(AddNewDriver_PanelLayout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addComponent(jLabel3, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addGroup(AddNewDriver_PanelLayout.createSequentialGroup()
                        .addGap(18, 18, 18)
                        .addComponent(jScrollPane2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(36, 36, 36)
                .addGroup(AddNewDriver_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(phone_Textfield, javax.swing.GroupLayout.PREFERRED_SIZE, 34, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel4, javax.swing.GroupLayout.PREFERRED_SIZE, 26, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(26, 26, 26)
                .addGroup(AddNewDriver_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                    .addGroup(AddNewDriver_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                        .addComponent(date_ComboBox, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addComponent(month_Combobox, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(year_Combobox, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(37, 37, 37)
                .addGroup(AddNewDriver_PanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(addDataToDatabase_Button)
                    .addComponent(jButton1, javax.swing.GroupLayout.PREFERRED_SIZE, 32, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButton2, javax.swing.GroupLayout.PREFERRED_SIZE, 33, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 258, javax.swing.GroupLayout.PREFERRED_SIZE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(AddNewDriver_Panel, javax.swing.GroupLayout.PREFERRED_SIZE, 1172, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(28, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addComponent(AddNewDriver_Panel, javax.swing.GroupLayout.PREFERRED_SIZE, 710, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void phone_TextfieldActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_phone_TextfieldActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_phone_TextfieldActionPerformed

    private void driverName_TextfieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_driverName_TextfieldKeyReleased
        // TODO add your handling code here:

        driverName_Textfield.setText(validator.stringValidator(driverName_Textfield.getText()).toUpperCase());

    }//GEN-LAST:event_driverName_TextfieldKeyReleased

    private void address_TextfieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_address_TextfieldKeyReleased
        // TODO add your handling code here:

        address_Textfield.setText(validator.stringValidator(address_Textfield.getText()).toUpperCase());
    }//GEN-LAST:event_address_TextfieldKeyReleased

    private void phone_TextfieldKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_phone_TextfieldKeyReleased
        // TODO add your handling code here:

        phone_Textfield.setText(validator.numberValidator(phone_Textfield.getText()).toUpperCase());
    }//GEN-LAST:event_phone_TextfieldKeyReleased

    private void addDataToDatabase_ButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_addDataToDatabase_ButtonActionPerformed
        // TODO add your handling code here:

        SessionFactory sessionFactory = com.JD.master.Hibernate.Master_HibernateUtil.getSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();

        driver_name = driverName_Textfield.getText();
        driver_Address = address_Textfield.getText();
        driver_Phone = phone_Textfield.getText();


        com.JD.master.Hibernate.DriverMaster driverMaster = new DriverMaster(driver_name, driver_Address, driver_Phone, rawField1, rawField2, rawField3, rawField4);

        session.save(driverMaster);
        transaction.commit();

        javax.swing.table.DefaultTableModel model = (javax.swing.table.DefaultTableModel) driver_Table.getModel();

        model.insertRow(0, new Object[]{driver_name, driver_Address, driver_Phone});



        driverName_Textfield.setText("");
        address_Textfield.setText("");
        phone_Textfield.setText("");


    }//GEN-LAST:event_addDataToDatabase_ButtonActionPerformed

    private void month_ComboboxActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_month_ComboboxActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_month_ComboboxActionPerformed

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        // TODO add your handling code here:

        //driver_Table

        new com.JD.ExportToExcel.ExportToExcel().saveToExcel(driver_Table);

    }//GEN-LAST:event_jButton1ActionPerformed

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        try {
            // TODO add your handling code here:
            driver_Table.print();
        } catch (PrinterException ex) {
           JOptionPane.showMessageDialog(null, "Please Cheack Your Printer..");
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new AddNewDriver().setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JPanel AddNewDriver_Panel;
    private javax.swing.JButton addDataToDatabase_Button;
    private javax.swing.JTextArea address_Textfield;
    private javax.swing.JComboBox date_ComboBox;
    private javax.swing.JTextField driverName_Textfield;
    private javax.swing.JTable driver_Table;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JComboBox month_Combobox;
    private javax.swing.JTextField phone_Textfield;
    private javax.swing.JComboBox year_Combobox;
    // End of variables declaration//GEN-END:variables
}
